package com.wits.qso.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;

import com.wits.platform.service.BaseService;
import com.wits.qso.dao.AidDao;
import com.wits.qso.entity.Aid;
import com.wits.qso.entity.Attachment;
import com.wits.qso.entity.CreContent;
import com.wits.qso.util.FileType;
import com.wits.qso.util.FileUtils;

@Component
public class AidService extends BaseService<Aid, String> {
	private AidDao aidDao;

	@Autowired
	public void setAidDao(AidDao aidDao) {
		this.baseDao=aidDao;
		this.aidDao = aidDao;
	}
	
	public List<Aid> findByCreContent(CreContent crecontent) {
		return aidDao.findByCreContent( crecontent );
	}
	
	/**
	 * 新增文件
	 */
	public List<Attachment> addFile(String realPath,List<MultipartFile> files){
		List<Attachment> fileList = new ArrayList<Attachment>();
		
		try {
			for (MultipartFile partFile : files){
				if (partFile.isEmpty()) {
					continue;
				}
				
				Attachment file = new Attachment();
				file.setFilePath(realPath);
				
				FileCopyUtils.copy(partFile.getBytes(),new java.io.File(file.getFilePath()+"\\"+partFile.getOriginalFilename()));
				//构建文件上传File实例并保存到数据库
				fileList.add(createFile(partFile, file));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return fileList;
	}
	
	/**
	 * 
	 *方法摘要：根据MultipartFile信息构建文件上传Attachment对象
	 *@param
	 *@return Attachment
	 */
	private Attachment createFile(MultipartFile partFile,Attachment file){
		String fileName = partFile.getOriginalFilename();//原始名称
		file.setOriginalName(fileName);
		//获取文件扩展名
		String stuffix = FileUtils.getFileExt(fileName);
		file.setExtName(stuffix.toLowerCase());
		file.setFileType(getFileTypeName(stuffix.toLowerCase()));
		
		file.setFileSize(partFile.getSize());
		Date date=new Date();
		file.setDateCre(date);
		file.setFilePath("upload\\"+fileName);
		file.setUploadTime(date);
		file.setLastModifyDate(date);
		file.setUploadTime(date);
		return file;
	}
	
	/**
	 * 方法摘要：获取文件类型名称
	 * 
	 * @param
	 * @return String
	 */
	private String getFileTypeName(String fileStuffix) {
		String fileTypeName = "";
		if (fileStuffix.equals(FileType.WORD2003.getStuffix())) {
			fileTypeName = FileType.WORD2003.getValue();
		} else if (fileStuffix.equals(FileType.WORD2007.getStuffix())) {
			fileTypeName = FileType.WORD2007.getValue();
		} else if (fileStuffix.equals(FileType.EXCEL2003.getStuffix())) {
			fileTypeName = FileType.EXCEL2003.getValue();
		} else if (fileStuffix.equals(FileType.EXCEL2007.getStuffix())) {
			fileTypeName = FileType.EXCEL2007.getValue();
		} else if (fileStuffix.equals(FileType.PPT2003.getStuffix())) {
			fileTypeName = FileType.PPT2003.getValue();
		} else if (fileStuffix.equals(FileType.PPT2007.getStuffix())) {
			fileTypeName = FileType.PPT2007.getValue();
		} else if (fileStuffix.equals(FileType.TXT.getStuffix())) {
			fileTypeName = FileType.TXT.getValue();
		} else if (fileStuffix.equals(FileType.JPG.getStuffix())) {
			fileTypeName = FileType.JPG.getValue();
		} else if (fileStuffix.equals(FileType.GIF.getStuffix())) {
			fileTypeName = FileType.GIF.getValue();
		} else if (fileStuffix.equals(FileType.PNG.getStuffix())) {
			fileTypeName = FileType.PNG.getValue();
		} else if (fileStuffix.equals(FileType.BMP.getStuffix())) {
			fileTypeName = FileType.BMP.getValue();
		}else if (fileStuffix.equals(FileType.RAR.getStuffix())) {
			fileTypeName = FileType.RAR.getValue();
		} else if (fileStuffix.equals(FileType.ZIP.getStuffix())) {
			fileTypeName = FileType.ZIP.getValue();
		} else {
			fileTypeName = "未知类型";
		}
		return fileTypeName;
	}
}
